package com.ang.controller;

import com.ang.base.AjaxResponse;
import com.ang.pojo.Article;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

import static org.springframework.web.bind.annotation.RequestMethod.*;

@Slf4j
@RestController
@RequestMapping("/rest")
public class ArticleRestController {

    @RequestMapping(value = "/article", method = POST, produces = "application/json")
    @Caching(evict = {@CacheEvict(cacheNames = "articleList", allEntries = true)},
            put = {@CachePut(cacheNames = "article")})
    public @ResponseBody
    AjaxResponse saveArticle(@RequestBody Article article) {
        //因为使用了lombok的Slf4j注解，这里可以直接使用log变量打印日志
        log.info("saveArticle：{}", article);
        return AjaxResponse.success(article);
    }


    //删除一篇Article，使用DELETE方法，参数是id
    @RequestMapping(value = "/article/{id}", method = DELETE, produces = "application/json")
    @Caching(evict = {@CacheEvict(cacheNames = "articleList", allEntries = true),
            @CacheEvict(cacheNames = "article", key = "#id")})
    public AjaxResponse deleteArticle(@PathVariable Long id) {
        log.info("deleteArticle：{}", id);
        return AjaxResponse.success(id);
    }

    //     //更新一篇Article，使用PUT方法，以id为主键进行更新
//    @RequestMapping(value = "/article/{id}", method = PUT, produces = "application/json")
//    public AjaxResponse updateArticle(@PathVariable Long id, @RequestBody Article article) {
//        article.setId(id);
//        log.info("updateArticle：{}",article);
//        return AjaxResponse.success(article);
//    }
    @RequestMapping(value = "/article/{id}", method = PUT, produces = "application/json")
    @Caching(evict = {@CacheEvict(cacheNames = "articleList", allEntries = true)},
            put = {@CachePut(cacheNames = "article", key = "#id")})
    public @ResponseBody
    AjaxResponse updateArticle(
            @Valid @RequestBody Article article, @PathVariable String id) {

        //业务
        return AjaxResponse.success(article);
    }

    //获取一篇Article，使用GET方法
//    @RequestMapping(value = "/article/{id}", method = GET, produces = "application/json")
//    public AjaxResponse getArticle(@PathVariable Long id) {
//
//        //使用lombok提供的builder构建对象
//        Article article1 = Article.builder()
//                                .id(1L)
//                                .author("zimug")
//                                .content("spring boot 2.深入浅出")
//                                .createTime(new Date())
//                                .title("t1").build();
//        return AjaxResponse.success(article1);
//    }
    @Cacheable(value = "article")
    @GetMapping("/article/{id}")
    public @ResponseBody
    AjaxResponse getArticle(@PathVariable Long id) {

        //使用lombok提供的builder构建对象
        Article article1 = Article.builder()
                .id(1L)
                .author("zimug")
                .content("spring boot 2.深入浅出")
                .createTime(new Date())
                .title("t1").build();
        return AjaxResponse.success(article1);
    }
}